package com.cskaoyan.service.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.wx.vo.WxCollectListVo;
import com.cskaoyan.mapper.CollectMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.service.CollectService;
import com.cskaoyan.utils.Constant;
import com.github.pagehelper.PageHelper;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    CollectMapper collectMapper;

    @Autowired
    GoodsMapper goodsMapper;


    @Override
    public BasePageData selectCollect(Byte type, Integer page, Integer limit) {

        Subject subject = SecurityUtils.getSubject();
        User primaryPrincipal = (User) subject.getPrincipals().getPrimaryPrincipal();
            Integer id1 = primaryPrincipal.getId();
            PageHelper.startPage(page , limit);
            List<WxCollectListVo> wxCollectListVos = new ArrayList<>();

            CollectExample collectExample = new CollectExample();
            CollectExample.Criteria criteria = collectExample.createCriteria();
            criteria.andTypeEqualTo(type).andDeletedEqualTo(false).andUserIdEqualTo(id1);
            List<Collect> collects = collectMapper.selectByExample(collectExample);
            for (Collect collect : collects) {
                //type为0，valueId则是商品id
                Integer valueId = collect.getValueId();
                Integer id = collect.getId();
                Goods goods = goodsMapper.selectByPrimaryKey(valueId);
                wxCollectListVos.add(new WxCollectListVo(goods.getBrief() , goods.getPicUrl() , valueId , goods.getName() , id , type , goods.getRetailPrice()));
            }
            return BasePageData.ok(wxCollectListVos);
    }

    @Override
    public int addOrDelete(Byte type, Integer valueId) {
        //获取当前用户

        Subject subject = SecurityUtils.getSubject();
        User primaryPrincipal = null;
        try {
            primaryPrincipal = (User) subject.getPrincipals().getPrimaryPrincipal();
        } catch (Exception e) {
            return Constant.FAIL;
        }

        Integer userId = primaryPrincipal.getId();

        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andValueIdEqualTo(valueId).andUserIdEqualTo(userId);
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        if (collects.size() != 0){
            for (Collect collect : collects) {
                Boolean deleted = collect.getDeleted();
                Integer id = collect.getId();
                if (!deleted){
                    collect.setDeleted(true);
                    collect.setUpdateTime(new Date());
                    CollectExample collectExample1 = new CollectExample();
                    CollectExample.Criteria criteria1 = collectExample1.createCriteria();
                    criteria1.andIdEqualTo(id);
                    int i = collectMapper.updateByExampleSelective(collect, collectExample1);
                    if (i == 1){
                        return Constant.SUCCESS;
                    }
                    return Constant.FAIL;
                }else {
                    collect.setDeleted(false);
                    collect.setUpdateTime(new Date());
                    CollectExample collectExample1 = new CollectExample();
                    CollectExample.Criteria criteria1 = collectExample1.createCriteria();
                    criteria1.andIdEqualTo(id);
                    int i = collectMapper.updateByExampleSelective(collect, collectExample);
                    if (i == 1){
                        return Constant.SUCCESS;
                    }
                    return Constant.FAIL;
                }
            }
        }
        //获取UserId

        Collect collect = new Collect();
        collect.setUserId(userId);
        collect.setValueId(valueId);
        collect.setType((byte) 0);
        collect.setAddTime(new Date());
        collect.setUpdateTime(new Date());
        collect.setDeleted(false);
        int count = collectMapper.insert(collect);
        if (count == 1){
            return Constant.SUCCESS;
        }
        return Constant.FAIL;
    }
}
